package cn.santi.controller;

import cn.santi.domain.*;
import cn.santi.emuns.R;
import cn.santi.jwt.JWTs;
import cn.santi.service.SearchService;
import cn.santi.service.SettingService;
import cn.santi.service.impl.SettingServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/staff")
public class StaffController {
    @Autowired
    private SettingService ss;
    @Autowired
    private SearchService ses;
    @GetMapping("/adminGetHomenakingList")
    public R adminGetHomenakingList(){
        List<StaffSetting> staffSettings  = ss.adminGetHomenakingList();
        System.out.println(staffSettings);
        for (int i = 0; i < staffSettings.size(); i++){
            List<SDate> staffDates = ss.getDate(staffSettings.get(i).getId());
            for (int j = 0; j < staffDates.size(); j++){
                List<StaffTime> staffTime = ss.getTime(staffDates.get(j).getId());
                staffDates.get(j).setTime(staffTime);
            }
            staffSettings.get(i).setMake(staffDates);
        }
        return R.ok(staffSettings);
    }
    @GetMapping("/adminUpDateHomemakingState")
    public R adminUpDateHomemakingState(int id,String operation){
        StaffSetting setting =  ss.getSettingById(id);
        if (operation.equals("启用")){
            setting.setState(0);
        }else if (operation.equals("停止预约(用户可见)")){
            setting.setState(1);
        }else if(operation.equals("关闭(用户不可见)")){
            setting.setState(2);
        }else{
            int i = ss.DeleteSetting(id);
            if (i == 1){
                return R.ok();
            }
            return R.fail();
        }
        int i = ss.UpDataSetting(setting);
        if (i == 1){
            return R.ok();
        }
        return R.fail();
    }
    @GetMapping("/adminSearchHomenaking")
    public R adminSearchHomenaking(String content){
        List<StaffSetting> staffSettings = ss.adminSearchHomenaking(content);
        for (int i = 0; i < staffSettings.size(); i++){
            List<SDate> staffDates = ss.getDate(staffSettings.get(i).getId());
            for (int j = 0; j < staffDates.size(); j++){
                List<StaffTime> staffTime = ss.getTime(staffDates.get(j).getId());
                staffDates.get(j).setTime(staffTime);
            }
            staffSettings.get(i).setMake(staffDates);
        }
        return R.ok(staffSettings);
    }
    @GetMapping("/adminGethomemakingClassify")
    public R adminGethomemakingClassify(String classify){
        List<StaffSetting> staffSettings = null;
        if (classify.equals("全部")){
            staffSettings = ss.adminGetHomenakingList();
        } else if(classify.equals("使用中")){
            staffSettings = ss.adminGethomemakingClassify(0);
        }else if(classify.equals("已停止")){
            staffSettings = ss.adminGethomemakingClassify(1);
        }else if(classify.equals("已关闭")){
            staffSettings = ss.adminGethomemakingClassify(2);
        }
        if (staffSettings.size() != 0){
            for (int i = 0; i < staffSettings.size(); i++){
                List<SDate> staffDates = ss.getDate(staffSettings.get(i).getId());
                for (int j = 0; j < staffDates.size(); j++){
                    List<StaffTime> staffTime = ss.getTime(staffDates.get(j).getId());
                    staffDates.get(j).setTime(staffTime);
                }
                staffSettings.get(i).setMake(staffDates);
            }
        }
        return R.ok(staffSettings);
    }
    //
    @GetMapping("/adminSearchUser")
    public R adminSearchUser(String content){
        List<User> searchHistories = ses.adminSearchUser(content);
        return R.ok(searchHistories);
    }
    @GetMapping("/adminAddSearch")
    public void adminAddSearch(String page,String token,String content){
        String id = JWTs.decode(token).replace("\"", ""); // 去除字符串中的所有双引号
        int idAsInt = Integer.parseInt(id); // 将字符串"1"转换为int类型的1
        SearchHistory searchHistory = new SearchHistory();
        searchHistory.setId(null);
        searchHistory.setPage(page);
        searchHistory.setSomeoneID(idAsInt);
        searchHistory.setContent(content);
        ses.addSearchHistory(searchHistory);
    }
    @GetMapping("/adminGetSearch")
    public R adminGetSearch(String token,String page){
        String id = JWTs.decode(token).replace("\"", ""); // 去除字符串中的所有双引号
        int idAsInt = Integer.parseInt(id); // 将字符串"1"转换为int类型的1
        List<SearchHistory> searchHistories = ses.adminGetSearch(idAsInt,page);
        return R.ok(searchHistories);
    }
    @GetMapping("/adminDeleteSearch")
    public void adminDeleteSearch(String token,String page,String content){
        String id = JWTs.decode(token).replace("\"", ""); // 去除字符串中的所有双引号
        int idAsInt = Integer.parseInt(id); // 将字符串"1"转换为int类型的1
        ses.adminDeleteSearch(idAsInt,page,content);
    }
    @GetMapping("/adminSearchAdmin")
    public R adminSearchAdmin(String content){
        List<Administrator> searchHistories = ses.adminSearchAdmin(content);
        return R.ok(searchHistories);
    }
    @GetMapping("/adminSearchLog")
    public R adminSearchLog(String content){
        List<adminLog> searchHistories = ses.adminSearchLog(content);
        return R.ok(searchHistories);
    }
}
